﻿using GodSystem.FormChildren;
using GodSystemCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem
{
    public partial class FormMain : Form
    {
       
        public FormMain()
        {
            InitializeComponent();
            // 默认显示首页
            display_home_form();
            // 初始化可见性
            panel_initialize();
        }

        private void panel_initialize()
        {
            // 用户信息下拉
            panel_userinfo_area.Visible = false;
            // 系统管理下拉
            panel_system_area.Visible = false;

            // 产品管理下拉
            panel_ProductManagerArea.Visible = false;



            // 最大化，最小化，关闭按钮提示
            ToolTip toolTip = new ToolTip();
            toolTip.ShowAlways = true;
            toolTip.RemoveAll();
            toolTip.SetToolTip(button_min, "最小化");
            toolTip.SetToolTip(button_max, "最大化/还原");
            toolTip.SetToolTip(button_close, "关闭");

            #region 左栏目系统管理，根据权限来展示可见性
            // 检测用户权限
            if (!Utility.CheckUserPower("UserList"))
            {
                button_system_userlist.Visible = false;
            }
            // 检测角色权限查看
            if (!Utility.CheckUserPower("RolePower"))
            {
                button_role_power.Visible = false;
            }
            #endregion

            #region 左栏目产品管理，根据权限来展示可见性
            // 分类查看
            if (!Utility.CheckUserPower("ProductCateCheck"))
            {
                button_Categories.Visible = false;
            }
            // 产品查看
            if (!Utility.CheckUserPower("ProductCheck"))
            {
                button_Products.Visible = false;
            }
            // 供应商查看
            if (!Utility.CheckUserPower("SupplierCheck"))
            {
                button_Supplier.Visible = false;
            }

            #endregion
        }

        /// <summary>
        /// 伸缩左侧子菜单下拉
        /// </summary>
        /// <param name="subMenu"></param>
        private void show_subMenu(Panel subMenu)
        {
            if(subMenu.Visible == false)
            {
                subMenu.Visible = true;
            }
            else
            {
                subMenu.Visible = false;
            }
        }


        // 移动窗口
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        /// <summary>
        /// 上面面板区可拖动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_top_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }


        private void button_warehouse_manage_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 初始化加载页面
        /// </summary>
        private void display_home_form()
        {
            FormHome formHome = new FormHome();
            openChildrenForm(formHome);
        }

        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        /// <summary>
        /// 最大化及还原原始大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_max_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;
            }
            else
            {
                // 只显示在主屏幕区，防止状态栏被覆盖
                MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                WindowState = FormWindowState.Maximized;
            }
        }

        /// <summary>
        /// 关闭应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 左菜单首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_leftmenu_home_Click(object sender, EventArgs e)
        {
            FormHome formHome = new FormHome();
            openChildrenForm(formHome);
        }

        /// <summary>
        /// 内容右区域窗体全部关闭，重新打开要操作的子窗体
        /// </summary>
        /// <param name="childForm"></param>
        private void openChildrenForm(Form childForm)
        {
            // 右内容窗体全部关闭
            foreach(Control control in panel_content_right.Controls)
            {
                if(control is Form)
                {
                    ((Form)control).Close();
                }
            }
            childForm.TopLevel = false;
            childForm.Parent = panel_content_right;
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            childForm.Activate();
            childForm.Show();
        }

        Button btn_temp = new Button();
        Button btn_temp_sub = new Button();

        /// <summary>
        /// 设置正在选择项的颜色展示
        /// </summary>
        /// <param name="selectedButton"></param>
        private void selectedBackGround(Button selectedButton)
        {
            btn_temp.BackColor = Color.FromArgb(57, 62, 70);
            btn_temp_sub.BackColor = Color.FromArgb(42, 54, 59);

            btn_temp = selectedButton;
            btn_temp.BackColor = Color.FromArgb(0, 120, 215);
        }

        /// <summary>
        /// 用户信息菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_sub_userinfo_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_sub_userinfo);
            show_subMenu(panel_userinfo_area);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_change_password_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_change_password);
            FormChangePassword changePassword = new FormChangePassword();
            openChildrenForm(changePassword);
        }

        #region 系统管理子菜单列表功能

        /// <summary>
        /// 系统管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_sub_system_manage_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_sub_system_manage);
            show_subMenu(panel_system_area);
        }



        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_system_userlist_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_system_userlist);
            FormUserList formUserList = new FormUserList();
            openChildrenForm(formUserList);
        }

        private void button_role_power_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_role_power);
            FormRolePower formRolePower = new FormRolePower();
            openChildrenForm(formRolePower);
        }

        #endregion


        #region 产品管理子菜单项

        /// <summary>
        /// 产品管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ProductManager_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_ProductManager);
            show_subMenu(panel_ProductManagerArea);
        }

        private void button_Categories_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_Categories);
            FormProductCate formProductCate = new FormProductCate();
            openChildrenForm(formProductCate);
        }

   
        private void button_Products_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_Products);
            FormProductList formProductList = new FormProductList();
            openChildrenForm(formProductList);
        }

        private void button_Supplier_Click(object sender, EventArgs e)
        {
            selectedBackGround(button_Supplier);
            FormSupplier formSupplier = new FormSupplier();
            openChildrenForm(formSupplier);
        }


        #endregion


    }
}
